<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 左侧欢迎区域 -->
      <div class="left-section">
        <div class="welcome-text">
          <h2>欢迎回来</h2>
          <p>高校勤工助学管理系统</p>
        </div>
        <div class="decoration">
          <img src="@/assets/login-decoration.svg" alt="decoration">
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="right-section">
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <h3 class="title">用户登录</h3>

          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              :prefix-icon="User"
            >
              <template #prefix>
                <el-icon><i-ep-user /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              :type="passwordVisible ? 'text' : 'password'"
              placeholder="密码"
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><i-ep-lock /></el-icon>
              </template>
              <template #suffix>
                <el-icon
                  class="cursor-pointer"
                  @click="passwordVisible = !passwordVisible"
                >
                  <i-ep-view v-if="passwordVisible" />
                  <i-ep-hide v-else />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="submit-btn"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>

          <div class="register-link">
            还没有账号？
            <router-link to="/register">立即注册</router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'

export default {
  name: 'LoginPage',
  setup() {
    const store = useStore()
    const loginFormRef = ref(null)
    const loading = ref(false)
    const passwordVisible = ref(false)

    const loginForm = ref({
      username: '',
      password: ''
    })

    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }

    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      try {
        await loginFormRef.value.validate()
        loading.value = true
        await store.dispatch('login', loginForm.value)
        ElMessage.success('登录成功')
      } catch (error) {
        console.error('Login error:', error)
        ElMessage.error(error.response?.data?.message || '登录失败')
      } finally {
        loading.value = false
      }
    }

    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      passwordVisible,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-box {
  width: 900px;
  height: 600px;
  background: #fff;
  border-radius: 15px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
}

.left-section {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: white;
}

.welcome-text {
  text-align: center;
}

.welcome-text h2 {
  font-size: 32px;
  margin-bottom: 10px;
  font-weight: 600;
}

.welcome-text p {
  font-size: 18px;
  opacity: 0.9;
}

.decoration {
  text-align: center;
}

.decoration img {
  width: 80%;
  max-width: 300px;
}

.right-section {
  flex: 1;
  padding: 40px;
  display: flex;
  align-items: center;
}

.login-form {
  width: 100%;
  max-width: 380px;
  margin: 0 auto;
}

.title {
  font-size: 28px;
  color: #333;
  text-align: center;
  margin-bottom: 40px;
  font-weight: 600;
}

.submit-btn {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  margin-top: 10px;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.register-link a {
  color: #667eea;
  text-decoration: none;
  margin-left: 5px;
  font-weight: 500;
}

.register-link a:hover {
  text-decoration: underline;
}

:deep(.el-input__wrapper) {
  padding: 0 11px;
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #667eea inset !important;
}

:deep(.el-input__inner) {
  height: 45px;
}

:deep(.el-form-item__content) {
  line-height: 45px;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  height: 45px;
  font-size: 16px;
  font-weight: 500;
}

:deep(.el-button--primary:hover) {
  opacity: 0.9;
  transform: translateY(-1px);
  transition: all 0.3s;
}

.cursor-pointer {
  cursor: pointer;
}
</style> 